<?php
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

include("include/dbcommon.php");
include("classes/searchclause.php");

add_nocache_headers();

include("include/proforma_invoices_variables.php");

if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"))
{
	echo "<p>".mlang_message("NO_PERMISSIONS")."<a href=\"login.php\">".mlang_message("BACK_TO_LOGIN")."</a></p>";
	return;
}

$all=postvalue("all");

$pageName = "print.php";

include('include/xtempl.php');
include('classes/runnerpage.php');
$xt = new Xtempl();

$id = postvalue("id") != "" ? postvalue("id") : 1;
//array of params for classes
$params = array("pageType" => PAGE_PRINT, "id" =>$id, "tName"=>$strTableName);
$params["xt"] = &$xt;
	
$pageObject = new RunnerPage($params);


// add onload event
$onLoadJsCode = GetTableData($pageObject->tName, ".jsOnloadPrint", '');
$pageObject->addOnLoadJsEvent($onLoadJsCode);

// add button events if exist
$pageObject->addButtonHandlers();



// Modify query: remove blob fields from fieldlist.
// Blob fields on a print page are shown using imager.php (for example).
// They don't need to be selected from DB in print.php itself.
if(!postvalue("pdf"))
	$gQuery->ReplaceFieldsWithDummies(GetBinaryFieldsIndices());

//	Before Process event
if($eventObj->exists("BeforeProcessPrint"))
	$eventObj->BeforeProcessPrint($conn);

$strWhereClause="";
$strHavingClause="";

$selected_recs=array();
if (@$_REQUEST["a"]!="") 
{
	
	$sWhere = "1=0";	
	
//	process selection
	if (@$_REQUEST["mdelete"])
	{
		foreach(@$_REQUEST["mdelete"] as $ind)
		{
			$keys=array();
			$keys["id"]=refine($_REQUEST["mdelete1"][mdeleteIndex($ind)]);
			$selected_recs[]=$keys;
		}
	}
	elseif(@$_REQUEST["selection"])
	{
		foreach(@$_REQUEST["selection"] as $keyblock)
		{
			$arr=explode("&",refine($keyblock));
			if(count($arr)<1)
				continue;
			$keys=array();
			$keys["id"]=urldecode($arr[0]);
			$selected_recs[]=$keys;
		}
	}

	foreach($selected_recs as $keys)
	{
		$sWhere = $sWhere . " or ";
		$sWhere.=KeyWhere($keys);
	}
	$strSQL = gSQLWhere($sWhere);
	$strWhereClause=$sWhere;
}
else
{
	$strWhereClause=@$_SESSION[$strTableName."_where"];
	$strHavingClause=@$_SESSION[$strTableName."_having"];
	$strSQL = gSQLWhere($strWhereClause, $strHavingClause);
}
if(postvalue("pdf"))
	$strWhereClause = @$_SESSION[$strTableName."_pdfwhere"];

$_SESSION[$strTableName."_pdfwhere"] = $strWhereClause;


$strOrderBy=$_SESSION[$strTableName."_order"];
if(!$strOrderBy)
	$strOrderBy=$gstrOrderBy;
$strSQL.=" ".trim($strOrderBy);

$strSQLbak = $strSQL;
if($eventObj->exists("BeforeQueryPrint"))
	$eventObj->BeforeQueryPrint($strSQL,$strWhereClause,$strOrderBy);

//	Rebuild SQL if needed

if($strSQL!=$strSQLbak)
{
//	changed $strSQL - old style	
	$numrows=GetRowCount($strSQL);
}
else
{
	$strSQL = gSQLWhere($strWhereClause, $strHavingClause);
	$strSQL.=" ".trim($strOrderBy);
	
	$rowcount=false;
	if($eventObj->exists("ListGetRowCount"))
	{
		$masterKeysReq=array();
		for($i = 0; $i < count($pageObject->detailKeysByM); $i ++)
			$masterKeysReq[]=$_SESSION[$strTableName."_masterkey".($i + 1)];
			$rowcount=$eventObj->ListGetRowCount($pageObject->searchClauseObj,$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs);
	}
	if($rowcount!==false)
		$numrows=$rowcount;
	else
	{
		$numrows = gSQLRowCount($strWhereClause, $strHavingClause);
	}
}

LogInfo($strSQL);

$mypage=(integer)$_SESSION[$strTableName."_pagenumber"];
if(!$mypage)
	$mypage=1;

//	page size
$PageSize=(integer)$_SESSION[$strTableName."_pagesize"];
if(!$PageSize)
	$PageSize = GetTableData($strTableName,".pageSize",0);

if($PageSize<0)
	$all = 1;	
	
$recno = 1;
$records = 0;	
$maxpages = 1;
$pageindex = 1;

if(!$all)
{	
	if($numrows)
	{
		$maxRecords = $numrows;
		$maxpages = ceil($maxRecords/$PageSize);
					
		if($mypage > $maxpages)
			$mypage = $maxpages;
		
		if($mypage < 1) 
			$mypage = 1;
		
		$maxrecs = $PageSize;
	}
	$listarray = false;
	if($eventObj->exists("ListQuery"))
		$listarray = $eventObj->ListQuery($pageObject->searchClauseObj,$_SESSION[$strTableName."_arrFieldForSort"],$_SESSION[$strTableName."_arrHowFieldSort"],$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs,$PageSize,$mypage);
	if($listarray!==false)
		$rs = $listarray;
	else
	{
			if($numrows)
		{
			$strSQL.=" limit ".(($mypage-1)*$PageSize).",".$PageSize;
		}
		$rs = db_query($strSQL,$conn);
	}
	
	//	hide colunm headers if needed
	$recordsonpage = $numrows-($mypage-1)*$PageSize;
	if($recordsonpage>$PageSize)
		$recordsonpage = $PageSize;
		
	$xt->assign("page_number",true);
	$xt->assign("maxpages",$maxpages);
	$xt->assign("pageno",$mypage);
}
else
{
	$listarray = false;
	if($eventObj->exists("ListQuery"))
		$listarray=$eventObj->ListQuery($pageObject->searchClauseObj,$_SESSION[$strTableName."_arrFieldForSort"],$_SESSION[$strTableName."_arrHowFieldSort"],$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs,$PageSize,$mypage);
	if($listarray!==false)
		$rs = $listarray;
	else
		$rs = db_query($strSQL,$conn);
	$recordsonpage = $numrows;
	$maxpages = ceil($recordsonpage/30);
	$xt->assign("page_number",true);
	$xt->assign("maxpages",$maxpages);
	
}


$fieldsArr = array();
$arr = array();
$arr['fName'] = "id";
$arr['viewFormat'] = ViewFormat("id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "account_id";
$arr['viewFormat'] = ViewFormat("account_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "account_address";
$arr['viewFormat'] = ViewFormat("account_address", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "buyer_id";
$arr['viewFormat'] = ViewFormat("buyer_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "buyer_address";
$arr['viewFormat'] = ViewFormat("buyer_address", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "consignee_id";
$arr['viewFormat'] = ViewFormat("consignee_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "consignee_address";
$arr['viewFormat'] = ViewFormat("consignee_address", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "notify_id";
$arr['viewFormat'] = ViewFormat("notify_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "notify_address";
$arr['viewFormat'] = ViewFormat("notify_address", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "proforma_invoice_no";
$arr['viewFormat'] = ViewFormat("proforma_invoice_no", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "proforma_invoice_date";
$arr['viewFormat'] = ViewFormat("proforma_invoice_date", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "customer_reference";
$arr['viewFormat'] = ViewFormat("customer_reference", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "customer_reference_date";
$arr['viewFormat'] = ViewFormat("customer_reference_date", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "commercial_invoice_no";
$arr['viewFormat'] = ViewFormat("commercial_invoice_no", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "commercial_invoice_date";
$arr['viewFormat'] = ViewFormat("commercial_invoice_date", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "term_of_sale1";
$arr['viewFormat'] = ViewFormat("term_of_sale1", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "term_of_sale2";
$arr['viewFormat'] = ViewFormat("term_of_sale2", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "shipment_mode_id";
$arr['viewFormat'] = ViewFormat("shipment_mode_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "estimated_shipment_date";
$arr['viewFormat'] = ViewFormat("estimated_shipment_date", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "estimated_shipment_date_custom";
$arr['viewFormat'] = ViewFormat("estimated_shipment_date_custom", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "additional_charges_frieght_type";
$arr['viewFormat'] = ViewFormat("additional_charges_frieght_type", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "additional_charges_legalization";
$arr['viewFormat'] = ViewFormat("additional_charges_legalization", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "additional_charges_inspection";
$arr['viewFormat'] = ViewFormat("additional_charges_inspection", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "additional_charges_special_packing";
$arr['viewFormat'] = ViewFormat("additional_charges_special_packing", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others1_key";
$arr['viewFormat'] = ViewFormat("others1_key", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others2_key";
$arr['viewFormat'] = ViewFormat("others2_key", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others3_key";
$arr['viewFormat'] = ViewFormat("others3_key", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others1_value";
$arr['viewFormat'] = ViewFormat("others1_value", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others2_value";
$arr['viewFormat'] = ViewFormat("others2_value", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "others3_value";
$arr['viewFormat'] = ViewFormat("others3_value", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "payment_term_id";
$arr['viewFormat'] = ViewFormat("payment_term_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "payment_term_other";
$arr['viewFormat'] = ViewFormat("payment_term_other", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "bank_name_id";
$arr['viewFormat'] = ViewFormat("bank_name_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "bank_term1";
$arr['viewFormat'] = ViewFormat("bank_term1", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "bank_term2";
$arr['viewFormat'] = ViewFormat("bank_term2", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "currency_id";
$arr['viewFormat'] = ViewFormat("currency_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "terms_and_conditions";
$arr['viewFormat'] = ViewFormat("terms_and_conditions", $strTableName);
$fieldsArr[] = $arr;
$pageObject->setGoogleMapsParams($fieldsArr);

$colsonpage=1;
if($colsonpage>$recordsonpage)
	$colsonpage=$recordsonpage;
if($colsonpage<1)
	$colsonpage=1;


//	fill $rowinfo array
	$pages = array();
	$rowinfo = array();
	$rowinfo["data"]=array();
	if($eventObj->exists("ListFetchArray"))
		$data = $eventObj->ListFetchArray($rs);
	else
		$data = db_fetch_array($rs);	

	while($data)
	{
		if($eventObj->exists("BeforeProcessRowPrint"))
		{
			if(!$eventObj->BeforeProcessRowPrint($data))
			{
				if($eventObj->exists("ListFetchArray"))
					$data = $eventObj->ListFetchArray($rs);
				else
					$data = db_fetch_array($rs);
				continue;
			}
		}
		break;
	}
	
	while($data && ($all || $recno<=$PageSize))
	{
		$row=array();
		$row["grid_record"]=array();
		$row["grid_record"]["data"]=array();
		for($col=1;$data && ($all || $recno<=$PageSize) && $col<=1;$col++)
		{
			$record=array();
			$recno++;
			$records++;
			$keylink="";
			$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["id"]));


//	id - 
			$value="";
				$value = ProcessLargeText(GetData($data,"id", ""),"field=id".$keylink,"",MODE_PRINT);
			$record["id_value"]=$value;

//	account_id - 
			$value="";
				$value=DisplayLookupWizard("account_id",$data["account_id"],$data,$keylink,MODE_PRINT);
			$record["account_id_value"]=$value;

//	account_address - 
			$value="";
				$value = ProcessLargeText(GetData($data,"account_address", ""),"field=account%5Faddress".$keylink,"",MODE_PRINT);
			$record["account_address_value"]=$value;

//	consignee_id - 
			$value="";
				$value=DisplayLookupWizard("consignee_id",$data["consignee_id"],$data,$keylink,MODE_PRINT);
			$record["consignee_id_value"]=$value;

//	consignee_address - 
			$value="";
				$value = ProcessLargeText(GetData($data,"consignee_address", ""),"field=consignee%5Faddress".$keylink,"",MODE_PRINT);
			$record["consignee_address_value"]=$value;

//	notify_id - 
			$value="";
				$value=DisplayLookupWizard("notify_id",$data["notify_id"],$data,$keylink,MODE_PRINT);
			$record["notify_id_value"]=$value;

//	notify_address - 
			$value="";
				$value = ProcessLargeText(GetData($data,"notify_address", ""),"field=notify%5Faddress".$keylink,"",MODE_PRINT);
			$record["notify_address_value"]=$value;

//	buyer_id - 
			$value="";
				$value=DisplayLookupWizard("buyer_id",$data["buyer_id"],$data,$keylink,MODE_PRINT);
			$record["buyer_id_value"]=$value;

//	buyer_address - 
			$value="";
				$value = ProcessLargeText(GetData($data,"buyer_address", ""),"field=buyer%5Faddress".$keylink,"",MODE_PRINT);
			$record["buyer_address_value"]=$value;

//	proforma_invoice_no - 
			$value="";
				$value = ProcessLargeText(GetData($data,"proforma_invoice_no", ""),"field=proforma%5Finvoice%5Fno".$keylink,"",MODE_PRINT);
			$record["proforma_invoice_no_value"]=$value;

//	proforma_invoice_date - Short Date
			$value="";
				$value = ProcessLargeText(GetData($data,"proforma_invoice_date", "Short Date"),"field=proforma%5Finvoice%5Fdate".$keylink,"",MODE_PRINT);
			$record["proforma_invoice_date_value"]=$value;

//	customer_reference - 
			$value="";
				$value = ProcessLargeText(GetData($data,"customer_reference", ""),"field=customer%5Freference".$keylink,"",MODE_PRINT);
			$record["customer_reference_value"]=$value;

//	commercial_invoice_no - 
			$value="";
				$value = ProcessLargeText(GetData($data,"commercial_invoice_no", ""),"field=commercial%5Finvoice%5Fno".$keylink,"",MODE_PRINT);
			$record["commercial_invoice_no_value"]=$value;

//	commercial_invoice_date - Short Date
			$value="";
				$value = ProcessLargeText(GetData($data,"commercial_invoice_date", "Short Date"),"field=commercial%5Finvoice%5Fdate".$keylink,"",MODE_PRINT);
			$record["commercial_invoice_date_value"]=$value;

//	customer_reference_date - Short Date
			$value="";
				$value = ProcessLargeText(GetData($data,"customer_reference_date", "Short Date"),"field=customer%5Freference%5Fdate".$keylink,"",MODE_PRINT);
			$record["customer_reference_date_value"]=$value;

//	payment_term_id - 
			$value="";
				$value=DisplayLookupWizard("payment_term_id",$data["payment_term_id"],$data,$keylink,MODE_PRINT);
			$record["payment_term_id_value"]=$value;

//	payment_term_other - 
			$value="";
				$value = ProcessLargeText(GetData($data,"payment_term_other", ""),"field=payment%5Fterm%5Fother".$keylink,"",MODE_PRINT);
			$record["payment_term_other_value"]=$value;

//	currency_id - 
			$value="";
				$value=DisplayLookupWizard("currency_id",$data["currency_id"],$data,$keylink,MODE_PRINT);
			$record["currency_id_value"]=$value;

//	bank_name_id - 
			$value="";
				$value=DisplayLookupWizard("bank_name_id",$data["bank_name_id"],$data,$keylink,MODE_PRINT);
			$record["bank_name_id_value"]=$value;

//	bank_term1 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"bank_term1", ""),"field=bank%5Fterm1".$keylink,"",MODE_PRINT);
			$record["bank_term1_value"]=$value;

//	bank_term2 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"bank_term2", ""),"field=bank%5Fterm2".$keylink,"",MODE_PRINT);
			$record["bank_term2_value"]=$value;

//	additional_charges_frieght_type - 
			$value="";
				$value = ProcessLargeText(GetData($data,"additional_charges_frieght_type", ""),"field=additional%5Fcharges%5Ffrieght%5Ftype".$keylink,"",MODE_PRINT);
			$record["additional_charges_frieght_type_value"]=$value;

//	additional_charges_legalization - 
			$value="";
				$value = ProcessLargeText(GetData($data,"additional_charges_legalization", ""),"field=additional%5Fcharges%5Flegalization".$keylink,"",MODE_PRINT);
			$record["additional_charges_legalization_value"]=$value;

//	additional_charges_inspection - 
			$value="";
				$value = ProcessLargeText(GetData($data,"additional_charges_inspection", ""),"field=additional%5Fcharges%5Finspection".$keylink,"",MODE_PRINT);
			$record["additional_charges_inspection_value"]=$value;

//	additional_charges_special_packing - 
			$value="";
				$value = ProcessLargeText(GetData($data,"additional_charges_special_packing", ""),"field=additional%5Fcharges%5Fspecial%5Fpacking".$keylink,"",MODE_PRINT);
			$record["additional_charges_special_packing_value"]=$value;

//	others1_key - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others1_key", ""),"field=others1%5Fkey".$keylink,"",MODE_PRINT);
			$record["others1_key_value"]=$value;

//	others1_value - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others1_value", ""),"field=others1%5Fvalue".$keylink,"",MODE_PRINT);
			$record["others1_value_value"]=$value;

//	others2_key - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others2_key", ""),"field=others2%5Fkey".$keylink,"",MODE_PRINT);
			$record["others2_key_value"]=$value;

//	others2_value - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others2_value", ""),"field=others2%5Fvalue".$keylink,"",MODE_PRINT);
			$record["others2_value_value"]=$value;

//	others3_key - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others3_key", ""),"field=others3%5Fkey".$keylink,"",MODE_PRINT);
			$record["others3_key_value"]=$value;

//	others3_value - 
			$value="";
				$value = ProcessLargeText(GetData($data,"others3_value", ""),"field=others3%5Fvalue".$keylink,"",MODE_PRINT);
			$record["others3_value_value"]=$value;

//	terms_and_conditions - 
			$value="";
				$value = ProcessLargeText(GetData($data,"terms_and_conditions", ""),"field=terms%5Fand%5Fconditions".$keylink,"",MODE_PRINT);
			$record["terms_and_conditions_value"]=$value;

//	term_of_sale1 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"term_of_sale1", ""),"field=term%5Fof%5Fsale1".$keylink,"",MODE_PRINT);
			$record["term_of_sale1_value"]=$value;

//	term_of_sale2 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"term_of_sale2", ""),"field=term%5Fof%5Fsale2".$keylink,"",MODE_PRINT);
			$record["term_of_sale2_value"]=$value;

//	shipment_mode_id - 
			$value="";
				$value=DisplayLookupWizard("shipment_mode_id",$data["shipment_mode_id"],$data,$keylink,MODE_PRINT);
			$record["shipment_mode_id_value"]=$value;

//	estimated_shipment_date - Short Date
			$value="";
				$value = ProcessLargeText(GetData($data,"estimated_shipment_date", "Short Date"),"field=estimated%5Fshipment%5Fdate".$keylink,"",MODE_PRINT);
			$record["estimated_shipment_date_value"]=$value;

//	estimated_shipment_date_custom - 
			$value="";
				$value = ProcessLargeText(GetData($data,"estimated_shipment_date_custom", ""),"field=estimated%5Fshipment%5Fdate%5Fcustom".$keylink,"",MODE_PRINT);
			$record["estimated_shipment_date_custom_value"]=$value;
			if($col<$colsonpage)
				$record["endrecord_block"]=true;
			$record["grid_recordheader"]=true;
			$record["grid_vrecord"]=true;
			
			if($eventObj->exists("BeforeMoveNextPrint"))
				$eventObj->BeforeMoveNextPrint($data,$row,$record);
				
			$row["grid_record"]["data"][]=$record;
			
			if($eventObj->exists("ListFetchArray"))
				$data = $eventObj->ListFetchArray($rs);
			else
				$data = db_fetch_array($rs);
				
			while($data)
			{
				if($eventObj->exists("BeforeProcessRowPrint"))
				{
					if(!$eventObj->BeforeProcessRowPrint($data))
					{
						if($eventObj->exists("ListFetchArray"))
							$data = $eventObj->ListFetchArray($rs);
						else
							$data = db_fetch_array($rs);
						continue;
					}
				}
				break;
			}
		}
		if($col<=$colsonpage)
		{
			$row["grid_record"]["data"][count($row["grid_record"]["data"])-1]["endrecord_block"]=false;
		}
		$row["grid_rowspace"]=true;
		$row["grid_recordspace"] = array("data"=>array());
		for($i=0;$i<$colsonpage*2-1;$i++)
			$row["grid_recordspace"]["data"][]=true;
		
		$rowinfo["data"][]=$row;
		
		if($all && $records>=30)
		{
			$page=array("grid_row" =>$rowinfo);
			$page["pageno"]=$pageindex;
			$pageindex++;
			$pages[] = $page;
			$records=0;
			$rowinfo=array();
		}
		
	}
	if(count($rowinfo))
	{
		$page=array("grid_row" =>$rowinfo);
		if($all)
			$page["pageno"]=$pageindex;
		$pages[] = $page;
	}
	
	for($i=0;$i<count($pages);$i++)
	{
	 	if($i<count($pages)-1)
			$pages[$i]["begin"]="<div name=page class=printpage>";
		else
		    $pages[$i]["begin"]="<div name=page>";
			
		$pages[$i]["end"]="</div>";
	}

	$page=array();
	$page["data"]=&$pages;
	$xt->assignbyref("page",$page);

	

$strSQL=$_SESSION[$strTableName."_sql"];

$isPdfView = true;
if (GetTableData($strTableName, ".isUsebuttonHandlers", false) || $isPdfView || $onLoadJsCode)
{
	$pageObject->body["begin"] .="<script type=\"text/javascript\" src=\"include/jquery.js\"></script>\r\n";
	$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";

	if ($pageObject->debugJSMode === true)
	{
		/*$pageObject->body['begin'] .= "<script type=\"text/javascript\" src=\"include/runnerJS/Runner.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Util.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Observer.js\"></script>\r\n";*/
	$pageObject->body['begin'] .= "<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
	}
	else
	{
		$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
	}	
	$pageObject->fillSetCntrlMaps();
	$pageObject->body['end'] .= '<script>';
	$pageObject->body['end'] .= "window.controlsMap = '".jsreplace(my_json_encode($pageObject->controlsHTMLMap))."';";
	$pageObject->body['end'] .= "window.settings = '".jsreplace(my_json_encode($pageObject->jsSettings))."';";
	$pageObject->body['end'] .= '</script>';
	$pageObject->addCommonJs();		
}

$pagename = $_SERVER["REQUEST_URI"];
if(!$pagename)
{
	$pagename=basename(__file__);
	$params="";
	foreach($_GET as $k=>$v)
	{
		if(strlen($params))
			$params.="&";
		$params.=rawurlencode($k)."=".rawurlencode($v);
	}
	if(strlen($params))
		$pagename.="?".$params;
}
if(strpos($pagename,"?")===false)
	$pagename.="?pdf=1";
else
	$pagename.="&pdf=1";


if(!postvalue("pdf"))
{
		$pageObject->AddJSFile("include/pdf");
	$xt->assign("pdflink_block",true);
	$xt->assign("pdflink_attrs","onclick=\"RunPDF(); this.style.display='none'; return false;\"");
	$pageObject->body["end"] .= "<script>
		var page = '".jsreplace($pagename)."';
		</script>
		<iframe src=\"javascript:false;\" name=\"pdf\" class=\"pdf\" style=\"position:absolute;left:-1000px;\">
		</iframe>";
}
$pdf_block = array("begin"=>"<div id=progress>", "end"=>"</div>");
if(count($pages))
	$pages[0]["pdf_block"]=&$pdf_block;

if (GetTableData($strTableName, ".isUsebuttonHandlers", false) || $isPdfView || $onLoadJsCode)
	$pageObject->body["end"] .= "<script>".$pageObject->PrepareJS()."</script>";

$xt->assignbyref("body",$pageObject->body);
$xt->assign("grid_block",true);

$xt->assign("id_fieldheadercolumn",true);
$xt->assign("id_fieldheader",true);
$xt->assign("id_fieldcolumn",true);
$xt->assign("id_fieldfootercolumn",true);
$xt->assign("account_id_fieldheadercolumn",true);
$xt->assign("account_id_fieldheader",true);
$xt->assign("account_id_fieldcolumn",true);
$xt->assign("account_id_fieldfootercolumn",true);
$xt->assign("account_address_fieldheadercolumn",true);
$xt->assign("account_address_fieldheader",true);
$xt->assign("account_address_fieldcolumn",true);
$xt->assign("account_address_fieldfootercolumn",true);
$xt->assign("consignee_id_fieldheadercolumn",true);
$xt->assign("consignee_id_fieldheader",true);
$xt->assign("consignee_id_fieldcolumn",true);
$xt->assign("consignee_id_fieldfootercolumn",true);
$xt->assign("consignee_address_fieldheadercolumn",true);
$xt->assign("consignee_address_fieldheader",true);
$xt->assign("consignee_address_fieldcolumn",true);
$xt->assign("consignee_address_fieldfootercolumn",true);
$xt->assign("notify_id_fieldheadercolumn",true);
$xt->assign("notify_id_fieldheader",true);
$xt->assign("notify_id_fieldcolumn",true);
$xt->assign("notify_id_fieldfootercolumn",true);
$xt->assign("notify_address_fieldheadercolumn",true);
$xt->assign("notify_address_fieldheader",true);
$xt->assign("notify_address_fieldcolumn",true);
$xt->assign("notify_address_fieldfootercolumn",true);
$xt->assign("buyer_id_fieldheadercolumn",true);
$xt->assign("buyer_id_fieldheader",true);
$xt->assign("buyer_id_fieldcolumn",true);
$xt->assign("buyer_id_fieldfootercolumn",true);
$xt->assign("buyer_address_fieldheadercolumn",true);
$xt->assign("buyer_address_fieldheader",true);
$xt->assign("buyer_address_fieldcolumn",true);
$xt->assign("buyer_address_fieldfootercolumn",true);
$xt->assign("proforma_invoice_no_fieldheadercolumn",true);
$xt->assign("proforma_invoice_no_fieldheader",true);
$xt->assign("proforma_invoice_no_fieldcolumn",true);
$xt->assign("proforma_invoice_no_fieldfootercolumn",true);
$xt->assign("proforma_invoice_date_fieldheadercolumn",true);
$xt->assign("proforma_invoice_date_fieldheader",true);
$xt->assign("proforma_invoice_date_fieldcolumn",true);
$xt->assign("proforma_invoice_date_fieldfootercolumn",true);
$xt->assign("customer_reference_fieldheadercolumn",true);
$xt->assign("customer_reference_fieldheader",true);
$xt->assign("customer_reference_fieldcolumn",true);
$xt->assign("customer_reference_fieldfootercolumn",true);
$xt->assign("commercial_invoice_no_fieldheadercolumn",true);
$xt->assign("commercial_invoice_no_fieldheader",true);
$xt->assign("commercial_invoice_no_fieldcolumn",true);
$xt->assign("commercial_invoice_no_fieldfootercolumn",true);
$xt->assign("commercial_invoice_date_fieldheadercolumn",true);
$xt->assign("commercial_invoice_date_fieldheader",true);
$xt->assign("commercial_invoice_date_fieldcolumn",true);
$xt->assign("commercial_invoice_date_fieldfootercolumn",true);
$xt->assign("customer_reference_date_fieldheadercolumn",true);
$xt->assign("customer_reference_date_fieldheader",true);
$xt->assign("customer_reference_date_fieldcolumn",true);
$xt->assign("customer_reference_date_fieldfootercolumn",true);
$xt->assign("payment_term_id_fieldheadercolumn",true);
$xt->assign("payment_term_id_fieldheader",true);
$xt->assign("payment_term_id_fieldcolumn",true);
$xt->assign("payment_term_id_fieldfootercolumn",true);
$xt->assign("payment_term_other_fieldheadercolumn",true);
$xt->assign("payment_term_other_fieldheader",true);
$xt->assign("payment_term_other_fieldcolumn",true);
$xt->assign("payment_term_other_fieldfootercolumn",true);
$xt->assign("currency_id_fieldheadercolumn",true);
$xt->assign("currency_id_fieldheader",true);
$xt->assign("currency_id_fieldcolumn",true);
$xt->assign("currency_id_fieldfootercolumn",true);
$xt->assign("bank_name_id_fieldheadercolumn",true);
$xt->assign("bank_name_id_fieldheader",true);
$xt->assign("bank_name_id_fieldcolumn",true);
$xt->assign("bank_name_id_fieldfootercolumn",true);
$xt->assign("bank_term1_fieldheadercolumn",true);
$xt->assign("bank_term1_fieldheader",true);
$xt->assign("bank_term1_fieldcolumn",true);
$xt->assign("bank_term1_fieldfootercolumn",true);
$xt->assign("bank_term2_fieldheadercolumn",true);
$xt->assign("bank_term2_fieldheader",true);
$xt->assign("bank_term2_fieldcolumn",true);
$xt->assign("bank_term2_fieldfootercolumn",true);
$xt->assign("additional_charges_frieght_type_fieldheadercolumn",true);
$xt->assign("additional_charges_frieght_type_fieldheader",true);
$xt->assign("additional_charges_frieght_type_fieldcolumn",true);
$xt->assign("additional_charges_frieght_type_fieldfootercolumn",true);
$xt->assign("additional_charges_legalization_fieldheadercolumn",true);
$xt->assign("additional_charges_legalization_fieldheader",true);
$xt->assign("additional_charges_legalization_fieldcolumn",true);
$xt->assign("additional_charges_legalization_fieldfootercolumn",true);
$xt->assign("additional_charges_inspection_fieldheadercolumn",true);
$xt->assign("additional_charges_inspection_fieldheader",true);
$xt->assign("additional_charges_inspection_fieldcolumn",true);
$xt->assign("additional_charges_inspection_fieldfootercolumn",true);
$xt->assign("additional_charges_special_packing_fieldheadercolumn",true);
$xt->assign("additional_charges_special_packing_fieldheader",true);
$xt->assign("additional_charges_special_packing_fieldcolumn",true);
$xt->assign("additional_charges_special_packing_fieldfootercolumn",true);
$xt->assign("others1_key_fieldheadercolumn",true);
$xt->assign("others1_key_fieldheader",true);
$xt->assign("others1_key_fieldcolumn",true);
$xt->assign("others1_key_fieldfootercolumn",true);
$xt->assign("others1_value_fieldheadercolumn",true);
$xt->assign("others1_value_fieldheader",true);
$xt->assign("others1_value_fieldcolumn",true);
$xt->assign("others1_value_fieldfootercolumn",true);
$xt->assign("others2_key_fieldheadercolumn",true);
$xt->assign("others2_key_fieldheader",true);
$xt->assign("others2_key_fieldcolumn",true);
$xt->assign("others2_key_fieldfootercolumn",true);
$xt->assign("others2_value_fieldheadercolumn",true);
$xt->assign("others2_value_fieldheader",true);
$xt->assign("others2_value_fieldcolumn",true);
$xt->assign("others2_value_fieldfootercolumn",true);
$xt->assign("others3_key_fieldheadercolumn",true);
$xt->assign("others3_key_fieldheader",true);
$xt->assign("others3_key_fieldcolumn",true);
$xt->assign("others3_key_fieldfootercolumn",true);
$xt->assign("others3_value_fieldheadercolumn",true);
$xt->assign("others3_value_fieldheader",true);
$xt->assign("others3_value_fieldcolumn",true);
$xt->assign("others3_value_fieldfootercolumn",true);
$xt->assign("terms_and_conditions_fieldheadercolumn",true);
$xt->assign("terms_and_conditions_fieldheader",true);
$xt->assign("terms_and_conditions_fieldcolumn",true);
$xt->assign("terms_and_conditions_fieldfootercolumn",true);
$xt->assign("term_of_sale1_fieldheadercolumn",true);
$xt->assign("term_of_sale1_fieldheader",true);
$xt->assign("term_of_sale1_fieldcolumn",true);
$xt->assign("term_of_sale1_fieldfootercolumn",true);
$xt->assign("term_of_sale2_fieldheadercolumn",true);
$xt->assign("term_of_sale2_fieldheader",true);
$xt->assign("term_of_sale2_fieldcolumn",true);
$xt->assign("term_of_sale2_fieldfootercolumn",true);
$xt->assign("shipment_mode_id_fieldheadercolumn",true);
$xt->assign("shipment_mode_id_fieldheader",true);
$xt->assign("shipment_mode_id_fieldcolumn",true);
$xt->assign("shipment_mode_id_fieldfootercolumn",true);
$xt->assign("estimated_shipment_date_fieldheadercolumn",true);
$xt->assign("estimated_shipment_date_fieldheader",true);
$xt->assign("estimated_shipment_date_fieldcolumn",true);
$xt->assign("estimated_shipment_date_fieldfootercolumn",true);
$xt->assign("estimated_shipment_date_custom_fieldheadercolumn",true);
$xt->assign("estimated_shipment_date_custom_fieldheader",true);
$xt->assign("estimated_shipment_date_custom_fieldcolumn",true);
$xt->assign("estimated_shipment_date_custom_fieldfootercolumn",true);

	$record_header=array("data"=>array());
	for($i=0;$i<$colsonpage;$i++)
	{
		$rheader=array();
		if($i<$colsonpage-1)
		{
			$rheader["endrecordheader_block"]=true;
		}
		$record_header["data"][]=$rheader;
	}
	$xt->assignbyref("record_header",$record_header);
	$xt->assign("grid_header",true);
	$xt->assign("grid_footer",true);


$templatefile = "proforma_invoices_print.htm";
	
if($eventObj->exists("BeforeShowPrint"))
	$eventObj->BeforeShowPrint($xt,$templatefile);

if(!postvalue("pdf"))
	$xt->display($templatefile);
else
{
	$xt->load_template($templatefile);
	$page = $xt->fetch_loaded();
	$pagewidth=postvalue("width")*1.05;
	$pageheight=postvalue("height")*1.05;
	$landscape=false;
	if(postvalue("all"))
	{
		if($pagewidth>$pageheight)
		{
			$landscape=true;
			if($pagewidth/$pageheight<297/210)
				$pagewidth = 297/210*$pageheight;
		}
		else
		{
			if($pagewidth/$pageheight<210/297)
				$pagewidth = 210/297*$pageheight;
		}
	}
	include("plugins/page2pdf.php");
}

?>
